/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jaiz.twitter.dao;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jaiz.twitter.model.DBManager;
import org.jaiz.twitter.model.Usuario;
import org.jaiz.twitter.util.Query;

/**
 *
 * @author Marin
 */
public class UsuarioDAO extends DBManager implements IDAO<Usuario, String> {    

    @Override
    public void create(Usuario u) {
        try {
            PreparedStatement statement = createStatement(Query.getQuery("usuario.create"));
            statement.setString(1, u.getUserName());
            statement.setString(2, u.getNombre());
            statement.setString(3, u.getApellido());
            statement.setString(5, u.getEmail());
            statement.setString(6, u.getPassword());
            statement.executeUpdate();
            
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    @Override
    public void delete(Usuario e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(Usuario e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Collection<Usuario> getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Usuario getById(String id) {
        try {
            PreparedStatement statement = createStatement(Query.getQuery("usuario.getByUserName"));
            statement.setString(1, id);            
            ResultSet rs = statement.executeQuery();
            if (rs.next()) { //Si hay registro
                Usuario usuario = new Usuario();
                usuario.setUserName(rs.getString("user_name"));
                usuario.setNombre(rs.getString("nombre"));
                usuario.setApellido(rs.getString("apellido"));
                usuario.setPassword(rs.getString("password"));
                usuario.setEmail(rs.getString("email"));
                return usuario;
            } else {
                return null;
            }
        } catch (SQLException ex) {
             Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
}
